<template>
  <div class="register-container">
    <div class="register-form">
      <h2>外卖平台注册</h2>
      <div class="form-group">
        <label for="username">用户名</label>
        <input 
          type="text" 
          id="username" 
          v-model="username" 
          placeholder="请设置用户名"
          required
        />
      </div>
      <div class="form-group">
        <label for="password">密码</label>
        <input 
          type="password" 
          id="password" 
          v-model="password" 
          placeholder="请设置密码"
          required
        />
      </div>
      <div class="form-group">
        <label for="confirmPassword">确认密码</label>
        <input 
          type="password" 
          id="confirmPassword" 
          v-model="confirmPassword" 
          placeholder="请再次输入密码"
          required
        />
      </div>
      <div class="form-group">
        <label for="phone">手机号</label>
        <input 
          type="tel" 
          id="phone" 
          v-model="phone" 
          placeholder="请输入手机号"
          required
        />
      </div>
      <button class="register-btn" @click="handleRegister">注册</button>
      <div class="login-link">
        已有账号？<router-link to="/login">立即登录</router-link>
      </div>
    </div>
  </div>
</template>

<script>
import { userAPI } from '../api/api.js';

export default {
  name: 'Register',
  data() {
    return {
      username: '',
      password: '',
      confirmPassword: '',
      phone: '',
      loading: false // 添加加载状态
    }
  },
  methods: {
    // 处理注册
    async handleRegister() {
      // 表单验证
      if (!this.username || !this.password || !this.phone || !this.confirmPassword) {
        alert('请填写所有必填字段');
        return;
      }

      if (this.password !== this.confirmPassword) {
        alert('两次输入的密码不一致');
        return;
      }

      if (!/^1[3-9]\d{9}$/.test(this.phone)) {
        alert('请输入正确的手机号');
        return;
      }

      this.loading = true;
      try {
        const userData = {
          username: this.username,
          password: this.password,
          phone: this.phone
        };

        // 调用真实后端注册API
        const response = await userAPI.register(userData);
        console.log('Register response:', response);
        
        // 注册成功后跳转到登录页面
        alert('注册成功，请登录');
        this.$router.push('/login');
      } catch (error) {
        console.error('Register error:', error);
        alert('注册失败: ' + (error.message || '请稍后重试'));
      } finally {
        this.loading = false;
      }
    }
  }
}
</script>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #f5f5f5;
}

.register-form {
  background-color: white;
  padding: 40px;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  width: 100%;
  max-width: 400px;
}

.register-form h2 {
  text-align: center;
  margin-bottom: 30px;
  color: #333;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  color: #666;
  font-size: 14px;
}

.form-group input {
  width: 100%;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 16px;
  transition: border-color 0.3s;
}

.form-group input:focus {
  outline: none;
  border-color: #1890ff;
}

.register-btn {
  width: 100%;
  padding: 12px;
  background-color: #52c41a;
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 16px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.register-btn:hover {
  background-color: #73d13d;
}

.login-link {
  text-align: center;
  margin-top: 20px;
  font-size: 14px;
  color: #666;
}

.login-link a {
  color: #1890ff;
  text-decoration: none;
}

.login-link a:hover {
  text-decoration: underline;
}
</style>